import axios from "@/tools/axios-config";
import { auth } from "@/tools/auth";


export const getCourses = () => {
    return axios.get("/courses/").then((res) => res.data);
};

export const getCourseById = (courseId) => {
    return axios.get(`/courses/${courseId}/`).then((res) => res.data);
};

export const getIntroductionByCourseId = (courseId) => {
    // 把数组转换成单个 object
    return axios.get(`/introductions/?course=${courseId}`).then((res) => {
        const [data] = res.data || [];
        return data;
    });
};
export const updateIntroductionById = ({ newIntroduction, introductionId, course }) => {
    return axios({
        method: "patch",
        url: `/introductions/${introductionId}/`,
        data: JSON.stringify({ content: newIntroduction, course: course }),
        headers: {
            'Content-type': 'application/json; charset=UTF-8',
            ...(auth.currentUser && {
                'Authorization': `Token ${auth.currentUser.token}`
            }),
        },
    }).then((res) => (res.data))
}